<%-- 
    Document   : pesquisarEspaco
    Created on : 20/10/2011, 22:55:01
    Author     : Maury
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="DAO.EnderecoDAO"%>
<%@page import="Model.Endereco"%>
<%@page import="java.util.ArrayList"%>
<%@page import="Exceptions.PrivilegioInsuficienteException"%>
<%@page import="Model.Usuario"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file = "autenticacaoFuncionario.jsp"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Sistema Controle de Reservas</title>
        <script type="text/javascript">
            function validar(form){
                var id = form.id.value;
                var num = form.num.value;
                document.getElementById("idAlert").innerHTML = "";
                document.getElementById("numAlert").innerHTML = "";
                
                if(id != ""){
                    id = Number(id);
                    if(isNaN(id)){
                        document.getElementById("idAlert").innerHTML = "Id inválido!";
                        form.id.focus();
                        return false;
                    }
                }
                else if(num != ""){
                    num = Number(num);
                    if(isNaN(num)){
                        document.getElementById("numAlert").innerHTML = "Número inválido!";
                        form.num.focus();
                        return false;
                    }
                }
                return true;                
            }
        </script>
    </head>
    <body>
        <%
            ArrayList<Endereco> enderecos = (ArrayList) request.getAttribute("enderecos");
            if(enderecos == null || enderecos.isEmpty()){
                enderecos = (ArrayList) session.getAttribute("enderecos");
                if(enderecos == null || enderecos.isEmpty()){
                    throw new Exception();
                }
            }
            session.setAttribute("enderecos", enderecos);
        
            String destino = request.getParameter("destino");
            if(destino == null){
                destino = (String) session.getAttribute("destino");
                if(destino == null){
                    throw new Exception();
                }
            }
            session.setAttribute("destino", destino);                    
            String link = null;
            
            if(destino.equals("remover")){
                link = "removerEspaco.jsp";
            }
            else if(destino.equals("alterar")){
                link = "alterarEspaco.jsp";
            }
            else if(destino.equals("listar")){
                 link= "listarEspaco.jsp";          
            }
            else{
                out.println("<p> Erro no sistema! </p>");
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
                rd.include(request, response); 
            }
        %>
        <h1>Pesquisar Espaço:</h1>
        <div id="message">${erro}</div>
        <form name="formSearch" method="post" action="MainAccessServlet" onsubmit="return validar(this)" accept-charset="iso-8859-1,utf-8">
            <input type="hidden" name="cmd" value="pesquisarEspaco"/>
            <input type="hidden" name="link" value="<%=link%>"/>
            Id do Espaço: <input type="text" name="id"/><br>
            <label id="idAlert"></label><br>
            <hr/>
            Número do Espaco: <input type="text" name="num"/><br>
            <label id="numAlert"></label><br>
            <select name="end">
                <c:forEach var="e" items="${enderecos}">
                    <option value="${e.id_endereco}">${e.cidade}, ${e.rua} ${e.numero}</option>
                </c:forEach>
            </select><br>
            <input type="submit" value="Pesquisar"/><input type="reset" value="Limpar"/>
            <% Usuario us = (Usuario) session.getAttribute("usuario");
            int idP=us.getPrivilegio().getId();
            if (idP==1){
            %>
            <button type="button" onclick="location.href='menuAdministrador.jsp'">Voltar</button><br>
            <% 
            }else if(idP==2){
            %>
            <button type="button" onclick="location.href='menuFuncionario.jsp'">Voltar</button><br>
            <%}else {%>
            <button type="button" onclick="location.href='menuAluno.jsp'">Voltar</button><br>
            <% }%>
        </form>
    </body>
</html>
